import {
  AlipayCircleOutlined,
  LockOutlined,
  MobileOutlined,
  TaobaoCircleOutlined,
  UserOutlined,
  WeiboCircleOutlined,
} from '@ant-design/icons';
import {Alert, message} from 'antd';
import React, {useState} from 'react';
import {LoginForm, ProFormCaptcha, ProFormCheckbox, ProFormText} from '@ant-design/pro-form';
import {history, useModel} from 'umi';
import Footer from '@/components/Footer'; //import { login } from '@/services/ant-design-pro/api';
import {getFakeCaptcha, login} from '@/services/ant-design-pro/login';
import styles from './index.less';

const LoginMessage: React.FC<{
  content: string;
}> = ({content}) => (
  <Alert
    style={{
      marginBottom: 24,
    }}
    message={content}
    type="error"
    showIcon
  />
);

const Login: React.FC = () => {
  const [userLoginState, setUserLoginState] = useState<API.LoginResult> ( {} );
  const [type, setType] = useState<string> ( 'account' );
  const [master] = useState<string> ( 'xxxxx' );
  const [loginUser, setLoginUser] = useState<API.LoginUser> ( {
    access: '',
    avatarUrl: '',
    country: '',
    geographic: {},
    nickName: '',
    openId: '',
    sex: 0,
    token: '',
  } );
  const {initialState, setInitialState} = useModel ( '@@initialState' );

  const fetchUserInfo = async () => {
    const userInfo = await initialState?.fetchUserInfo?. ();

    if (userInfo) {
      await setInitialState ( (s) => ({...s, currentUser: userInfo}) );
    }
  };

  const handleSubmit = async (values: API.LoginParams) => {
    try {
      // 登录
      const msg = await login ( {...values, type, master} );

      if (msg.status == 200) {
        var data = msg.data;
        localStorage.setItem ( 'token', data.token );
        setLoginUser ( msg.data );
        console.log ( data );
        const defaultLoginSuccessMessage = '登录成功！';
        message.success ( defaultLoginSuccessMessage );
        await fetchUserInfo ();
        /** 此方法会跳转到 redirect 参数所在的位置 */
        localStorage.setItem ( "movedate", new Date ().getTime () );
        if (!history) return;
        const {query} = history.location;
        const {redirect} = query as {
          redirect: string;
        };
        history.push ( redirect || '/' );
        return;
      } else {
        message.error ( msg.msg )
      }

      console.log ( msg ); // 如果失败去设置用户错误信息

      // @ts-ignore
      setUserLoginState ( {status: msg.success} );
    } catch (error) {
      const defaultLoginFailureMessage = '登录失败，请重试！';
      message.error ( defaultLoginFailureMessage );
    }
  };

  const {status, type: loginType} = userLoginState;
  return (
    <div className={styles.container}>
      <div className={styles.content}>
        <LoginForm
          logo={<img alt="logo" src="/logo.svg"/>}

          title="圆规管理系统"
          subTitle={'科学技术是人类对自然和社会进行探索和改造的手段和方法'}
          initialValues={{
            autoLogin: true,
          }}
          actions={[
            '其他登录方式 :',
            <AlipayCircleOutlined key="AlipayCircleOutlined" className={styles.icon}/>,
            <TaobaoCircleOutlined key="TaobaoCircleOutlined" className={styles.icon}/>,
            <WeiboCircleOutlined key="WeiboCircleOutlined" className={styles.icon}/>,
          ]}
          onFinish={async (values) => {
            await handleSubmit ( values as API.LoginParams );
          }}
        >
          {/*   <Tabs activeKey={type} onChange={setType}>
            <Tabs.TabPane key="account" tab={'账户密码登录'}/>
            <Tabs.TabPane key="mobile" tab={'手机号登录'}/>
          </Tabs>*/}

          {status === 'error' && loginType === 'account' && (
            <LoginMessage content={'错误的用户名和密码(admin/ant.design)'}/>
          )}
          {type === 'account' && (
            <>
              <ProFormText
                name="userName"
                fieldProps={{
                  size: 'large',
                  prefix: <UserOutlined className={styles.prefixIcon}/>,
                }}
                placeholder={'请输入账号'}
                rules={[
                  {
                    required: true,
                    message: '用户名是必填项！',
                  },
                ]}
              />
              <ProFormText.Password
                name="password"
                fieldProps={{
                  size: 'large',
                  prefix: <LockOutlined className={styles.prefixIcon}/>,
                }}
                placeholder={'请输入密码'}
                rules={[
                  {
                    required: true,
                    message: '密码是必填项！',
                  },
                ]}
              />
            </>
          )}

          {status === 'error' && loginType === 'mobile' && <LoginMessage content="验证码错误"/>}
          {type === 'mobile' && (
            <>
              <ProFormText
                fieldProps={{
                  size: 'large',
                  prefix: <MobileOutlined className={styles.prefixIcon}/>,
                }}
                name="mobile"
                placeholder={'请输入手机号！'}
                rules={[
                  {
                    required: true,
                    message: '手机号是必填项！',
                  },
                  {
                    pattern: /^1\d{10}$/,
                    message: '不合法的手机号！',
                  },
                ]}
              />
              <ProFormCaptcha
                fieldProps={{
                  size: 'large',
                  prefix: <LockOutlined className={styles.prefixIcon}/>,
                }}
                captchaProps={{
                  size: 'large',
                }}
                placeholder={'请输入验证码！'}
                captchaTextRender={(timing, count) => {
                  if (timing) {
                    return `${count} ${'秒后重新获取'}`;
                  }

                  return '获取验证码';
                }}
                name="captcha"
                rules={[
                  {
                    required: true,
                    message: '验证码是必填项！',
                  },
                ]}
                onGetCaptcha={async (phone) => {
                  const result = await getFakeCaptcha ( {
                    phone,
                  } );

                  if (result === false) {
                    return;
                  }

                  message.success ( '获取验证码成功！验证码为：1234' );
                }}
              />
            </>
          )}
          <div
            style={{
              marginBottom: 24,
            }}
          >
            <ProFormCheckbox noStyle name="autoLogin">
              自动登录
            </ProFormCheckbox>
            <a
              style={{
                float: 'right',
              }}
            >
              忘记密码 ?
            </a>
          </div>
        </LoginForm>
      </div>
      <Footer/>
    </div>
  );
};

export default Login;
